<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
if (isset($_SESSION['lang']))
    require_once "./language_files/offlineTemplate_" . $_SESSION['lang'] . ".php";
else
    require_once "./language_files/offlineTemplate_en.php";
?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=9" />
        <link href="style3.css" rel="stylesheet" type="text/css" />
        <link href="jquery-ui-all.css" rel="stylesheet" type="text/css" />
        <?php
        require_once 'functions.php';
        require_once "statics.php";
        $conf_id = $_GET['conference_id'];
        $paper = $_GET['paper_id'];
        $confName = get_conference_name($conf_id);
        $member_id = $_SESSION['member_id'];
        $verID = get_latest_version($paper);
        echo"
	</br><span class=breadcrumbs><a href='subscriberConferencePage.php?conference_id={$conf_id }'>" . $confName . "</a>" . " > " . $lang['offline'] . " </span> </br></br></br>";
        ?>
        <?php require_once 'jquery.php'; ?>
        <script type="text/javascript" src="jquery-ui-slider.js"></script>
        <script type="text/javascript" src="functions.js"></script>
        <title> <?php echo $lang['offline']; ?> </title>
        <script type="text/javascript" src="jqueryui-draggable.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                check_top();
                win = top.document.getElementById("collabsoft");
                $(win).load(function(){
                    $("#view").height($(window).height());
                });
                $("#v").click(function(){
                    $("#content").slideToggle();
                });
                
                /*$(function() {
                    $( ".draggable" ).draggable({ scroll: true, containment: "parent", 
                        start: function (event, ui) {
                            $(this).addClass("drag");
                        }
                    });
                });*/
                $(".NA").live("click", function(){
                    td = $(this).parent("td");
                    tr = $(td).parent("tr");
                    div= $(tr).find("div");
                    label = $(div).parent("td").find("label");
                    if($(this).is(':checked')) {
                        $(div).slider({ disabled: true, value: 0 });
                        $(div).attr("rate", "-1");
                        rate = $(div).attr("rate");
                        confID = $(div).attr("confID");
                        critID = $(div).attr("criteria");
                        paperID = $(div).attr("paperID");
                        label = $(div).parent("td").find("label");
                        td = $(this).parent("td");
                        tr = $(td).parent("tr");
                        span = $(tr).find("span");
                        $(label).html("New rate: "+ rate);
                        $(span).fadeIn("slow");
                        $.ajax ({
                            data:{rate:rate, confID:confID, critID:critID, paperID:paperID},
                            type: 'POST',
                            url:  'update_rate.php',
                            success: function(response) {
                                //alert(response)
                                $(span).delay(500).fadeOut('slow');
                            }
                        });
                    } else {
                        $(div).slider({ disabled: false, value: 0 });
                        $(div).attr("rate", "0");
                        $(label).html("New rate: "+ $(div).attr("rate"));
                    }
                });
                $(".slider").each(function(){
                    $(this).slider({ animate: true,
                        value: parseInt($(this).attr("preRate")),
                        min: 0,
                        max: 5,
                        step: 1,
                        change: function( event, ui ) {
                            
                            $(this).attr("rate", ui.value);
                            rate = $(this).attr("rate");
                            confID = $(this).attr("confID");
                            critID = $(this).attr("criteria");
                            paperID = $(this).attr("paperID");
                            label = $(this).parent("td").find("label");
                            $(label).html("New rate: "+ rate);
                            td = $(this).parent("td");
                            tr = $(td).parent("tr");
                            span = $(tr).find("span");
                            $(span).fadeIn("slow");
                            $.ajax ({
                                data:{rate:rate, confID:confID, critID:critID, paperID:paperID},
                                type: 'POST',
                                url:  'update_rate.php',
                                success: function(response) {
                                    //alert(response)
                                    $(span).delay(500).fadeOut('slow');
                                    //$(span).html("");
                                    
                                }
                            });
                        } 
                        
                    });
                    if($(this).attr("preRate") == -1) {
                        $(this).slider({ disabled: true }); 
                        td = $(this).parent("td");
                        tr = $(td).parent("tr");
                        box= $(tr).find(":checkbox");
                        $(box).attr("checked",true);
                    }   
                });
                $("#save").bind("click", function(){
                    comments = $("#comments").val();
                    paperID = $(this).attr("paperID");
                    type = $("#comments").attr("t");
                    $.ajax ({
                        data:{type:type, comments:comments, paperID:paperID},
                        type: 'POST',
                        url:  'insert.php',
                        success: function(response) {
                            if(response == 1) {
                                alert("Your review have been saved successfully");
                                location.reload();
                            } else if(response == 0) {
                                alert("all fields are required");
                            } else if(response == 2 ) {
                                alert("Your review have been updated successfully");
                                location.reload();
                            } else {
                                alert(response);
                            }
                        }
                    });
                    if(comments != "") {
                        $("#comments").addClass("disabled");
                        $("#comments").attr("disabled", true);
                    }
                });
                $("#edit").bind("click",function(){
                    $("#comments").removeClass("disabled");
                    $("#comments").attr("disabled", false);
                    $("#temp").attr("disabled", false);
                    $("#temp").removeClass("disabledButton");
                    $(this).addClass("disabledButton");
                    $(this).attr("disabled", true);
                });
                $("#temp").bind("click", function(){
                    $("#comments").insertAtCaret("Page ... (From line .. to line ..): ");
                });
                jQuery.fn.extend({
                    insertAtCaret: function(myValue){
                        return this.each(function(i) {
                            if (document.selection) {
                                //For browsers like Internet Explorer
                                this.focus();
                                sel = document.selection.createRange();
                                sel.text = myValue;
                                this.focus();
                            }
                            else if (this.selectionStart || this.selectionStart == '0') {
                                //For browsers like Firefox and Webkit based
                                var startPos = this.selectionStart;
                                var endPos = this.selectionEnd;
                                var scrollTop = this.scrollTop;
                                this.value = this.value.substring(0, startPos)+myValue+this.value.substring(endPos,this.value.length);
                                this.focus();
                                this.selectionStart = startPos + myValue.length;
                                this.selectionEnd = startPos + myValue.length;
                                this.scrollTop = scrollTop;
                            } else {
                                this.value += myValue;
                                this.focus();
                            }
                        })
                    }
                });

            });

        </script>
        <style>
            #v {
                background-color: #003366;
                color: white;
            }
            #v:hover {
                color: #003366;
                background-color: white;
            }
            .drag {
                width: 350px;
                height: fit-content;
                background-color: white;
                overflow: visible;
                cursor: move;
            }
            #saving {
                font-size: 10px;
                display: none;
                float: bottom;
            }

        </style>
    </head>
    <body>

        <?php
        ?> <label> <?php echo $lang['message_1'] . get_reviewDeadline($conf_id); ?></label> <?
        $old_value = mysql_query("SELECT text FROM comment c
                    INNER JOIN version v ON (c.version_id = v.version_id)
                    WHERE c.span_id = 0 AND v.paper_id = $paper AND c.commentor_id = $member_id") or die(mysql_error());
        if (mysql_num_rows($old_value) > 0) {
            $old = mysql_fetch_assoc($old_value);
            ?>
            <fieldset><legend id="v" style="cursor: pointer;" align="center">Show/Hide pdf</legend>
                <div id="content"><iframe id="view" style="width: 99%;  border:1px; border-radius: 10px; 
                                          padding-left: 0; padding-right: 0; padding-bottom: 0;
                                          margin-bottom: 0; margin-left: 0; margin-right: 0;" 
                                          src="viewSubmissions.php?paper_id=<?php echo $paper; ?>&conference_fid=<?php echo $conf_id; ?>">
                    </iframe></div></fieldset><br /><br />
            <fieldset class="draggable"><legend>Review box</legend>
                <label class="float_left">Please enter your review text here:</label><br />
                <textarea t="update" class="disabled" disabled="true" id="comments" style="width: 99%" rows="7" cols="40" name="comments"
                          value=""><?php echo $old['text']; ?></textarea> <button disabled="true" class="disabledButton" type="button" id="temp">Add review template</button><br />
                          <?
                          $all_criteria = rsSelect("rate_id", "conference_rating_criteria", array("conference_id=", $conf_id));
                          if (!is_string($all_criteria)) {
                              $Rating_Criteria = "";
                              echo '<table class="no_border" style="width:60%;">';
                              while ($row = mysql_fetch_assoc($all_criteria)) {
                                  $rsCriteriaName = rsSelect("criteria_type", "rating_criteria", array('rate_id=', $row['rate_id']));
                                  $rateid = $row['rate_id'];
                                  if (!is_string($rsCriteriaName)) {
                                      $rowCriteriaName = mysql_fetch_assoc($rsCriteriaName);
                                      $irate = "-";
                                      $old_rate = mysql_query("SELECT r.rating_value FROM version_rating_criteria r
                                                   WHERE r.span_id = 0 AND r.rate_id= $rateid AND r.version_id = $verID
                                                      AND member_id = $member_id") or die(mysql_error());
                                      if (mysql_num_rows($old_rate) > 0) {
                                          while ($r = mysql_fetch_assoc($old_rate)) {
                                              $irate = $r['rating_value'];
                                          }
                                      }
                                      ?><tr id="tr"><td width="10%"><label><?php echo $rowCriteriaName['criteria_type'] . ":" ?></label>
                                </td><td width="5%"><input type="checkbox" id="NA" class="NA"/><label style="cursor: pointer;" class="NAlabel">NA</label></td>
                                <td width="35%"><label class="sliderValue"><?php
                echo "Your previous rate: ";
                echo $irate;
                                      ?></label><div type="" paperID="<?php echo $paper ?>" preRate="<?php echo intval($irate); ?>"
                                                 criteria="<?php echo $row['rate_id']; ?>" confID="<?php echo $conf_id; ?>" rate='' class='slider'>
                                    </div></td><td width="10%" class="save"><span id="saving"><img src='images/saving.gif' alt='Saving...' />&nbsp;<label>Saving...</label></span></td></tr>
                            <?
                        }
                        else
                            echo $rsCriteriaName;
                    }
                    ?><tr>
                        <td><input paperID="<? echo $paper; ?>" type="button" name="save" value="save" id="save"/></td>
                        <td><button id="edit" name="edit" type="button">Edit</button></td>
                    </tr><?
                    echo $Rating_Criteria;
                }
                else
                    echo $all_criteria;
                ?> </table></fieldset> <?
        }

        else {
                ?>
            <fieldset><legend id="v" style="cursor: pointer;" align="center">Show/Hide pdf</legend>
                <div id="content"><iframe height="50" id="view" style="width: 99%; border:1px; border-radius: 10px;
                                          padding-left: 0; padding-right: 0; padding-bottom: 0; margin-bottom: 0; margin-left: 0; margin-right: 0;" src="viewSubmissions.php?paper_id=<?php echo $paper; ?>&conference_fid=<?php echo $conf_id; ?>">
                    </iframe></div></fieldset><br /><br />
            <fieldset class="draggable"><legend>Review box</legend>
                <label class="float_left">Please enter your review text here:</label><br />
                <textarea t="insert" id="comments" style="width: 99%" rows="7" cols="40" name="comments"
                          value=""></textarea><button type="button" id="temp">Add review template</button> <br />
                          <?
                          $all_criteria = rsSelect("rate_id", "conference_rating_criteria", array("conference_id=", $conf_id));
                          if (!is_string($all_criteria)) {
                              $Rating_Criteria = "";
                              echo '<table class="no_border" style="width:60%;">';
                              while ($row = mysql_fetch_assoc($all_criteria)) {
                                  $rateid = $row['rate_id'];
                                  $irate = "-";
                                  $rsCriteriaName = rsSelect("criteria_type", "rating_criteria", array('rate_id=', $row['rate_id']));
                                  $old_rate = mysql_query("SELECT r.rating_value FROM version_rating_criteria r
                                                   WHERE r.span_id = 0 AND r.rate_id= $rateid AND r.version_id = $verID
                                                  AND member_id = $member_id") or die(mysql_error());
                                  if (mysql_num_rows($old_rate) > 0) {
                                      while ($r = mysql_fetch_assoc($old_rate)) {
                                          $irate = $r['rating_value'];
                                      }
                                  }
                                  if (!is_string($rsCriteriaName)) {
                                      $rowCriteriaName = mysql_fetch_assoc($rsCriteriaName);
                                      ?><trv id="tr"><td width="10%"><label><?php echo $rowCriteriaName['criteria_type'] . ":" ?></label>
                                </td><td width="5%"><input type="checkbox" id="NA" class="NA"/><label style="cursor: pointer;" for="NA">NA</label></td>
                                <td width="35%"><label class="sliderValue"><?php
                echo "Your previous rate: ";
                echo $irate;
                                      ?></label>
                                    <div type="" paperID="<?php echo $paper ?>" preRate="0"
                                         criteria="<?php echo $row['rate_id']; ?>" confID="<?php echo $conf_id; ?>"
                                         rate='' class='slider'></div></td>
                                <td width="10%"><span id="saving" class="save"><img src='images/saving.gif' alt='Saving...' />&nbsp;<label>Saving...</label></span></td></tr><?
                    }
                    else
                        echo $rsCriteriaName;
                }
                              ?><tr>
                            <td class="float_left"><input paperID="<? echo $paper; ?>" type="button" name="save" 
                                                          value="save" id="save"/></td>
                        </tr><?
                echo $Rating_Criteria;
            }
            else
                echo $all_criteria;
                          ?> </table></fieldset> <?
            }
                      ?>

        </table>
    </body>
</html>